package com.myflink.day03;

import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.api.java.tuple.Tuple3;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.KeyedStream;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;


/**
 * @author Shelly An
 * @create 2020/9/18 10:26
 */
public class Operator_RollingAgg {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);

        DataStreamSource<String> fileDS = env.socketTextStream("localhost",4444);
        SingleOutputStreamOperator<Tuple3<String, Long, Integer>> sensorDS = fileDS.map(new MapFunction<String, Tuple3<String, Long, Integer>>() {

            @Override
            public Tuple3<String, Long, Integer> map(String value) throws Exception {
                String[] datas = value.split(",");
                //开发中，用包装类多一些。 parse返回的是基本类型
                return new Tuple3<>(datas[0], Long.valueOf(datas[1]), Integer.valueOf(datas[2]));
            }
        });

        KeyedStream<Tuple3<String, Long, Integer>, String> sensorKS = sensorDS.keyBy(r -> r.f0);
        //非累加字段保留第一条 SumAggregator
        sensorKS.sum(2).print("sum");
        sensorKS.max(2).print("max");


        env.execute();
    }
}
